package com.xxx.controller;

import com.xxx.domain.*;
import com.xxx.dto.CourseDto;
import com.xxx.service.ICourseService;
import com.xxx.query.CourseQuery;
import com.xxx.result.JSONResult;
import com.xxx.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.xxx.untils.LoginContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * 根据课程预创订单号获取订单确认页的填充信息kill_info/47847878
     */
    @RequestMapping(value = "/kill_info/{orderNo}", method = RequestMethod.GET)
    public JSONResult killInfo(@PathVariable("orderNo") String orderNo) {
        return JSONResult.success(courseService.killInfo(orderNo));
    }



    /**
     * 课程详情
     */
    @GetMapping("/detail/data/{courseId}")
    public JSONResult detail(@PathVariable("courseId") @Valid@NotNull(message = "课程ID不能为空") Long courseId) {
        return JSONResult.success(courseService.detail(courseId));
    }

    /**
     * 课程发布
     */
    @GetMapping("/onLineCourse/{courseId}")
    public JSONResult onlineCourse(@PathVariable("courseId") String courseId) {
        courseService.onlineCourse(courseId);
        return JSONResult.success();
    }


    /**
     * 添加课程
     */
    @RequestMapping(value = "/addCourse", method = RequestMethod.POST)
    public JSONResult addCourse(@RequestBody CourseDto courseDto) {
        courseService.addCourse(courseDto);
        return JSONResult.success();
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Course course) {
        if (course.getId() != null) {
            courseService.updateById(course);
        } else {
            courseService.insert(course);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @PreAuthorize("hasAuthority('course:list')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        Login login = LoginContext.getLogin();
        login.getId();
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        page = courseService.selectPage(page);
        return JSONResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/info/{courseIds}", method = RequestMethod.GET)
    public JSONResult info(@PathVariable("courseIds") @Valid@NotNull(message = "课程id不能为空") String courseIds) {
        return JSONResult.success(courseService.info(courseIds));
    }

}
